<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                修改密码
            </el-header>
            <el-main>
                <el-form ref="pwd" :rules="rules" :model="pwd" label-width="120px" :label-position="formAlignLeft"
                         style="padding: 10px;">
                    <el-form-item label="旧密码 :" prop="oldPw">
                        <el-input v-model="pwd.oldPw" type="password" size="small" placeholder="请输入旧密码"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码 :" prop="newPw">
                        <el-input v-model="pwd.newPw" type="password" size="small" placeholder="请输入新密码"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码 :" prop="againPw">
                        <el-input v-model="pwd.againPw" type="password" size="small" placeholder="请再次输入新密码"></el-input>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        完成修改
                    </el-button>
                    <el-button @click="editOut">
                        取消修改
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../../utils/webUtils.js';

    export default {
        name: "chgPwd",
        data() {
            // <!--验证密码-->
            let validatePass = (rule, value, callback) => {
                if (value === "") {
                    callback(new Error("请输入密码"))
                } else {
                    if (this.pwd.againPw !== "") {
                        this.$refs.pwd.validateField("checkPass");
                    }
                    callback()
                }
            };
            // <!--二次验证密码-->
            let validatePass2 = (rule, value, callback) => {
                if (value === "") {
                    callback(new Error("请再次输入密码"));
                } else if (value !== this.pwd.newPw) {
                    callback(new Error("两次输入密码不一致!"));
                } else {
                    callback();
                }
            };
            return {
                formAlignLeft: 'left',
                uuid: this.userUuid,
                pwd: {
                    oldPw: '',
                    newPw: '',
                    againPw: ''
                },
                rules: {
                    oldPw: [
                        {required: true, message: '请输入旧密码', trigger: 'blur'},
                    ],
                    newPw: [
                        {required: true, validator: validatePass, trigger: 'change'},
                    ],
                    againPw: [
                        {required: true, validator: validatePass2, trigger: 'change'},
                    ]
                },
                info: '',
                infoShow: false
            };
        },
        props: {
            userUuid: {
                type: String
            }
        },
        //深度监听父组件的props传值
        watch: {
            userUuid(val) {
                this.uuid = val;
            },
            other: {
                //深度观察对象
                handler(val) {
                },
                deep: true
            }
        },
        mounted() {
        },
        methods: {
            submit() {
                this.$refs['pwd'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.axios.request({
                        method: 'put',
                        url: './kwp/kdi/user/passwd/' + this.uuid,
                        headers: {
                            "Content-Type": "application/x-www-form-urlencoded"
                        },
                        params: replaceSpacesObj({
                            oldPw: this.pwd.oldPw,
                            newPw: this.pwd.newPw
                        })
                    }).then(response => {
                        console.log(response.data);
                        this.info = '修改成功,一秒后关闭';
                        this.infoShow = true;
                        setTimeout(() => {
                            this.infoShow = false;
                            this.editBoxShow = false;
                            this.cleanPwd();
                            this.info = '';
                            this.$emit("editState", false);
                        }, 1000);
                    }).catch(error => {
                        console.log(error);
                        this.info = '修改失败';
                        this.infoShow = true;
                    })
                });
            },
            cleanPwd() {
                this.pwd = {
                    oldPw: '',
                    newPw: '',
                    againPw: ''
                }
            },
            editOut() {
                this.infoShow = false;
                this.cleanPwd();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 360px;
        height: 100%;
        z-index: 100;
    }


</style>